<template>
  <div class="company-info-container">
    <div class="company-info-content">
      <!-- 搜索条件 -->
      <el-form 
        ref="searchFormRef"
        :model="searchForm" 
        class="search-form"
        inline
      >
        <el-input 
          v-model="searchForm.companyName" 
          placeholder="企业名称"
          clearable
          style="width: 200px"
        />
        
        <el-date-picker
          v-model="searchForm.afterTime"
          type="datetime"
          placeholder="开始时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 180px"
        />
        <span class="date-separator">至</span>
        <el-date-picker
          v-model="searchForm.beforeTime"
          type="datetime"
          placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 180px"
        />
        
        <el-input 
          v-model="searchForm.industry" 
          placeholder="所在行业"
          clearable
          style="width: 150px"
        />
        
        <el-select 
          v-model="searchForm.peopleSize" 
          placeholder="企业规模"
          clearable
          style="width: 150px"
        >
          <el-option label="0-20人" value="0-20人" />
          <el-option label="20-99人" value="20-99人" />
          <el-option label="100-499人" value="100-499人" />
          <el-option label="500-999人" value="500-999人" />
          <el-option label="1000-9999人" value="1000-9999人" />
          <el-option label="10000人以上" value="10000人以上" />
        </el-select>
        
        <el-select 
          v-model="searchForm.isVip" 
          placeholder="VIP状态"
          clearable
          style="width: 150px"
        >
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
        
        <el-button type="primary" @click="handleSearch" :loading="loading">
          <el-icon><Search /></el-icon>
          搜索
        </el-button>
        <el-button @click="handleReset">
          <el-icon><Refresh /></el-icon>
          重置
        </el-button>
      </el-form>
      
      <!-- 数据列表 -->
      <div class="table-header">
        <h3>企业信息列表</h3>
        <div class="table-actions">
          <el-button type="primary" text @click="refreshData" :loading="loading">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </div>
      
      <div class="table-container">
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          stripe
          style="width: 100%"
          max-height="500"
          size="small"
        >
          <el-table-column prop="logo" label="企业Logo" width="80" align="center">
            <template #default="{ row }">
              <el-avatar 
                :src="row.logo" 
                :size="40"
                class="company-logo"
              />
            </template>
          </el-table-column>
          
          <el-table-column label="企业信息" min-width="300">
            <template #default="{ row }">
              <div class="company-details">
                <div class="company-name">{{ row.companyName }}</div>
                <div class="company-short">{{ row.shortName }}</div>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column prop="industry" label="所在行业" width="120" />
          
          <el-table-column prop="peopleSize" label="企业规模" width="120" />
          
          <el-table-column label="VIP状态" width="100">
            <template #default="{ row }">
              <el-tag 
                :type="getVipStatusType(row.isVip)"
                size="small"
              >
                {{ getVipStatusText(row.isVip) }}
              </el-tag>
            </template>
          </el-table-column>
          
          <el-table-column label="操作" width="150" fixed="right">
            <template #default="{ row }">
              <el-button 
                type="primary" 
                text 
                size="small"
                @click="handleView(row)"
              >
                查看详情
              </el-button>
              <el-button 
                type="success" 
                text 
                size="small"
                @click="handleEdit(row)"
              >
                编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="searchForm.pageNo"
            v-model:page-size="searchForm.pageSize"
            :page-sizes="[10, 20, 50, 100]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :pager-count="5"
            small
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'
import { getCompanyInfoListApi } from '@/api/enterprise'

// 响应式数据
const loading = ref(false)
const tableData = ref([])
const total = ref(0)

// 搜索表单
const searchForm = reactive({
  companyName: '',
  beforeTime: '',
  afterTime: '',
  industry: '',
  peopleSize: '',
  isVip: null,
  pageNo: 1,
  pageSize: 10
})

// 表单引用
const searchFormRef = ref()

// 方法
const getVipStatusType = (status) => {
  const statusMap = {
    0: 'info',      // 未发起 - 灰色
    1: 'success',   // 通过 - 绿色
    2: 'danger',    // 失败 - 红色
    3: 'warning'    // 审核中 - 橙色
  }
  return statusMap[status] || 'info'
}

const getVipStatusText = (status) => {
  const statusMap = {
    0: '未发起',
    1: '通过',
    2: '失败',
    3: '审核中'
  }
  return statusMap[status] || '未知'
}

const handleSearch = async () => {
  searchForm.pageNo = 1
  await fetchData()
}

const handleReset = () => {
  searchFormRef.value?.resetFields()
  searchForm.companyName = ''
  searchForm.beforeTime = ''
  searchForm.afterTime = ''
  searchForm.industry = ''
  searchForm.peopleSize = ''
  searchForm.isVip = null
  searchForm.pageNo = 1
  searchForm.pageSize = 10
  fetchData()
}

const handleSizeChange = (size) => {
  searchForm.pageSize = size
  searchForm.pageNo = 1
  fetchData()
}

const handleCurrentChange = (page) => {
  searchForm.pageNo = page
  fetchData()
}

const handleView = (row) => {
  ElMessage.info(`查看企业详情：${row.companyName}`)
  // TODO: 实现查看详情功能
}

const handleEdit = (row) => {
  ElMessage.info(`编辑企业信息：${row.companyName}`)
  // TODO: 实现编辑功能
}

const refreshData = () => {
  fetchData()
}

const fetchData = async () => {
  try {
    loading.value = true
    
    // 过滤空值
    const params = {}
    Object.keys(searchForm).forEach(key => {
      if (searchForm[key] !== '' && searchForm[key] !== null && searchForm[key] !== undefined) {
        params[key] = searchForm[key]
      }
    })
    
    const response = await getCompanyInfoListApi(params)
    
    if (response && response.list) {
      tableData.value = response.list
      total.value = response.totals || 0
    } else {
      tableData.value = []
      total.value = 0
    }
  } catch (error) {
    console.error('获取企业信息列表失败:', error)
    ElMessage.error('获取数据失败')
    tableData.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 生命周期
onMounted(() => {
  fetchData()
})
</script>

<style lang="scss" scoped>
.company-info-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: calc(100vh - 60px);
  display: flex;
  flex-direction: column;
}

.company-info-content {
  background: white;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  min-height: calc(100vh - 120px);
  
  .search-form {
    margin-bottom: 15px;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    
    .date-separator {
      margin: 0 5px;
      color: #909399;
    }
  }
  
  .table-header {
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    h3 {
      margin: 0;
      color: #303133;
      font-size: 16px;
    }
  }
  
  .table-container {
    display: flex;
    flex-direction: column;
    min-height: 600px;
    
    .el-table {
      flex: 1;
      margin-bottom: 0;
      width: 100%;
      overflow-x: auto;
      
      .el-table__body-wrapper {
        overflow-x: auto;
      }
      
      .company-logo {
        display: block;
        margin: 0 auto;
      }
      
      .company-details {
        .company-name {
          font-weight: 500;
          color: #303133;
          margin-bottom: 2px;
          font-size: 14px;
        }
        
        .company-short {
          font-size: 12px;
          color: #909399;
        }
      }
    }
    
    .pagination-container {
      display: flex;
      justify-content: center;
      padding: 15px 0;
      margin-top: 15px;
      border-top: 1px solid #f0f0f0;
      flex-shrink: 0;
      
      .el-pagination {
        flex-wrap: wrap;
        justify-content: center;
        min-width: 0;
        
        .el-pagination__total,
        .el-pagination__sizes,
        .el-pagination__jump {
          margin: 0 8px;
        }
        
        .el-pager {
          margin: 0 8px;
        }
      }
    }
  }
}

// 响应式处理
@media (max-width: 1200px) {
  .company-info-content {
    .search-form {
      .el-form-item {
        margin-bottom: 15px;
      }
    }
    
    .pagination-container {
      .el-pagination {
        .el-pagination__total,
        .el-pagination__sizes,
        .el-pagination__jump {
          margin: 0 4px;
        }
        
        .el-pager {
          margin: 0 4px;
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .company-info-content {
    padding: 15px;
    
    .search-form {
      .el-form-item {
        display: block;
        margin-bottom: 15px;
        
        .el-form-item__label {
          display: block;
          margin-bottom: 8px;
        }
      }
    }
    
    .pagination-container {
      .el-pagination {
        flex-direction: column;
        gap: 10px;
        
        .el-pagination__total,
        .el-pagination__sizes,
        .el-pagination__jump {
          margin: 0;
        }
        
        .el-pager {
          margin: 0;
        }
      }
    }
  }
}
</style>

